package com.neuedu.springmvc.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * RequestMapping 默认支持所有http 方法的
 */
@Controller
@RequestMapping("/user")
public class UserController {


    /**
     * ModelAndView 设置返回数据(要在视图中渲染大数据) 和视图的名字
     * http://localhost:8080/springmvc05/user/list
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/list")
    public ModelAndView list(HttpServletRequest request , HttpServletResponse response){

        ModelAndView modelAndView = new ModelAndView();

        //模拟从数据库中查询了10个用户
        List list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }

        modelAndView.addObject("list",list);
        //modelAndView.setViewName("/WEB-INF/jsp/user/user_list.jsp");
        modelAndView.setViewName("/user/user_list");
        return modelAndView;
        
    }


    /***
     * http://localhost:8080/springmvc05/user/toEdit
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/toEdit")
    public ModelAndView toEdit(HttpServletRequest request , HttpServletResponse response){

        ModelAndView modelAndView = new ModelAndView();
        Map user = new HashMap<>();


        user.put("username","jshand");
        user.put("age","18");
        user.put("addres","HLJ");

        modelAndView.addObject("user",user);
        modelAndView.setViewName("/user/user_edit");
        return modelAndView;

    }




    /**
     * method 控制当前的方法 允许什么样的http method 访问
     * http://localhost:8080/springmvc05/user/doPost
     * @return
     */
    //@RequestMapping(value="/doGet",method={ RequestMethod.GET, RequestMethod.POST})
    @RequestMapping(value="/doPost",method={  RequestMethod.POST})
    public ModelAndView doPost(){

        ModelAndView modelAndView = new ModelAndView();

        //模拟从数据库中查询了10个用户
        List list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }

        modelAndView.addObject("list",list);
        modelAndView.setViewName("/user/user_list");
        return modelAndView;

    }


    /**
     * 只支持get  请求
     * @return
     */
    @GetMapping("/doGet")
    public ModelAndView doGet(){

        ModelAndView modelAndView = new ModelAndView();

        //模拟从数据库中查询了10个用户
        List list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }

        modelAndView.addObject("list",list);
        modelAndView.setViewName("/user/user_list");
        return modelAndView;

    }






    /**
     *
     * http://localhost:8080/springmvc05/user/getParam?username=admin&password=123456
     *
     * HTTP状态 400 - 错误的请求 参数传递的有问题，
     * params = "username" 参数必须有username参数   Parameter conditions "username" not met for actual request parameters: password={123456}
     * params = "!age"   参数中不能包含age参数    Parameter conditions "username, !age" not met for actual request parameters: username={abac}, age={18}
     * params = "password=123456"  必须包含password参数，并且参数的值要求得等于123456     Parameter conditions "username, !age, password=123456" not met for actual request parameters: username={admin}, password={1234561}
     * params = "password!=123456" 如果包含password参数则不能等于123456       消息 Parameter conditions "username, !age, password!=123456" not met for actual request parameters: username={admin}, password={123456}
     * 想要接收参数
     * @return
     */
    @RequestMapping(value = "/getParam",params = {"username","!age","password!=123456"})
    public ModelAndView getParam(String username,String password){


        //不需要手动接收参数
        //String username = request.getParameter("username");
        //String password = request.getParameter("password");


        System.out.println("username = " + username);
        System.out.println("password = " + password);


        ModelAndView modelAndView = new ModelAndView();

        //模拟从数据库中查询了10个用户
        List list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }

        modelAndView.addObject("list",list);
        modelAndView.setViewName("/user/user_list");
        return modelAndView;

    }


    /***
     * userAgent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36
     * 如果userAgent 不满足， 则 HTTP状态 404 - 未找到
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/userHeader",headers={"User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"})
    public ModelAndView userHeader(String username,String password){


        System.out.println("username = " + username);
        System.out.println("password = " + password);


        ModelAndView modelAndView = new ModelAndView();

        //模拟从数据库中查询了10个用户
        List list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }

        modelAndView.addObject("list",list);
        modelAndView.setViewName("/user/user_list");
        return modelAndView;

    }


    /**
     * http://localhost:8080/springmvc05/user/aba/req1?username=admin&password=123456
     * @return
     */
    @RequestMapping(value = "/**/req1")
    public ModelAndView req1(){

        ModelAndView modelAndView = new ModelAndView();

        //模拟从数据库中查询了10个用户
        List list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }

        modelAndView.addObject("list",list);
        modelAndView.setViewName("/user/user_list");
        return modelAndView;

    }




}
